﻿using LxBasic;
using LxCore;
using LxCore.Samples;
using Microsoft.Win32;
using System;
using System.Diagnostics;

namespace LxCFD
{
    class WorkSpacePanelVM : JsObject
    {
        public WorkSpacePanelVM()
        {
            OpenFile = new DelegateCommand(openFile);
            OpenSamples = new DelegateCommand(openSamples);
            ExportSimFile = new DelegateCommand(saveJsonFile);
        }

        public LxInfo Info { get => LxSim.Sim.Info; }
        public DelegateCommand OpenFile { get; set; }
        public DelegateCommand OpenSamples { get; set; }
        public DelegateCommand ExportSimFile { get; set; }
        private void openFile()
        {
            try
            {
                OpenFileDialog openDialog = new OpenFileDialog();
#if DEBUG
                openDialog.InitialDirectory = Info.Directory;
#else
                openDialog.InitialDirectory = string.IsNullOrEmpty(Info.Directory) ? Environment.CurrentDirectory : Info.Directory;
#endif
                openDialog.Title = "打开文件";

                //设置打开文件类型
                openDialog.Filter = "json files (*.json)|*.json|msh files (*.msh)|*.msh|All files (*.*)|*.*";
                openDialog.FilterIndex = 1;

                //监测文件是否存在
                openDialog.CheckFileExists = true;

                //文件打开后执行以下程序
                if (openDialog.ShowDialog() == true)
                {
                    LxSim.Sim.FileManage.ReadFile(openDialog.FileName);
                }
            }
            catch (Exception e)
            {
                LxLog.Error(e.Message);
            }
        }
        private void openSamples()
        {
            var win = new SampleWindow();
            win.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            win.ShowDialog();
        }
        private void saveJsonFile()
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "json files (*.json)|*.json";
            if (!string.IsNullOrEmpty(LxSim.Sim.Info.Directory))
            {
                saveFileDialog.InitialDirectory = LxSim.Sim.Info.Directory;
            }
            if (!string.IsNullOrEmpty(LxSim.Sim.Info.ProjName))
            {
                saveFileDialog.FileName = LxSim.Sim.Info.ProjName;
            }
            if (saveFileDialog.ShowDialog() == true)
            {
                string path = saveFileDialog.FileName;
                LxSim.Sim.FileManage.WriteJsonFile(path);
            }
        }

        public DelegateCommand Test { get; set; } = new DelegateCommand(() =>
        {
            SolidCuboidHeatConduction heatConduction = new SolidCuboidHeatConduction();
            heatConduction.RunCase();
        });
    }
}
